×
☰ See All Chapters

Spring Data JPA Transactional Annotation

By default all the methods of CrudRepository are transactional. . They are annotated with @Transactional annotation with default settings in implementation class at runtime. For any read operations by default readOnly flag is set to true.

To override default transactional settings of any CrudRepository methods we need to override that method in our interface and annotate with @Transactional using required configurations.

Below example shows how we can annotate using @Transactional annotation to override the default transactional settings of any CrudRepository methods.

Example:

import org.springframework.data.domain.Pageable;

import org.springframework.data.repository.CrudRepository;

import org.springframework.transaction.annotation.Transactional;

 

public interface  StudentRepository extends CrudRepository<Student, Long>{

        @Transactional(timeout = 20)

        List<Student> findByMarksGreaterThan(Long marks);

}

Here we have configured timeout as 20 seconds to execute query without readOnly flag for findByMarksGreaterThan() method.

 

                             


All Chapters
Author